<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License edit by kim
 */

class b2c_member_consignee{

  //保存修改编辑后的收货地址
	public function purchase_save_cons($arr_post=array(),$member_id,&$msg=''){
        //检查必填信息是否存在
        if( !$this->check_cons_post($arr_post,$member_id,$msg) ) return false;
		$arr_post = utils::_filter_input($arr_post);
		foreach($arr_post as $k=>$v){
			$arr_post[$k] = strip_tags($v);
		}

		if($arr_post['mobile'] && !empty($arr_post['mobile'])){
			//验证手机格式
			$phone_setting = $arr_post['phone_setting'];
			$phones=app::get('b2c')->getConf('setting-phone.phone');
			foreach($phones as $v){
				$bool  = in_array($phone_setting,$v);
				if($bool){
					$len = $v['length'];
					break;
				}
			}

			if((!preg_match('/^[1-9]\d{'.($len-1).'}$/', $arr_post['mobile']))){
				$msg = app::get('b2c')->_('手机格式错误');
                return false;
			}
		}
		
        $member_cons_model = app::get('b2c')->model('member_consignee');
        /*需要存储的数据*/
        $save_data = array(
            'member_id' => $member_id,
            'name' => $arr_post['name'],
            'tel'  => $arr_post['tel'],
            'mobile' => $arr_post['mobile'],
            'phone_setting' => $arr_post['phone_setting'],
        );

        $row = $member_cons_model->getList('consignee_id',$save_data);
        if($row && $row[0]['consignee_id'] != $arr_post['consignee_id'] ){
            $msg = app::get('b2c')->_('收货人重复');
			return false;
        }

        if(!$arr_post['consignee_id']){
            if($member_cons_model->count(array('member_id'=>$member_id)) == 6){
                $msg = app::get('b2c')->_('最多只能添加6个地址，请先删除一条地址之后再添加');
                return false;
            }
        }

        /*订单如果是新增收货地址，设置为默认地址
         *会员中心根据提交数据来是否设置为默认地址
         */
        if(!$arr_post['consignee_id'] && !isset($arr_post['def_person']) ){
            $save_data['def_person'] = 1;
        }else{
            $save_data['def_person']  = $arr_post['def_person'];
            $consignee_id = $save_data['consignee_id'] = $arr_post['consignee_id'];
        }

        //save
        if(!$update_cons_id = $member_cons_model->set_default_cons($save_data,$consignee_id,$member_id,$msg)){
            return false;
        }
        $save_data['consignee_id'] = $update_cons_id;
        return $save_data;
	}

    //验证收货人必填参数是否存在
    function check_cons_post($arr_post,$member_id,&$msg=''){
        if(empty($arr_post) || empty($member_id)){
            $msg = app::get('b2c')->_('缺少参数');
			return false;
        }
        if(!$arr_post['name']){
            $msg = app::get('b2c')->_('收货人姓名未填写');
			return false;
        }
//        if(!$arr_post['passortNo']){
//            $msg = app::get('b2c')->_('护照未填写');
//            return false;
//        }
        
        if(!$arr_post['mobile'] && !$arr_post['tel']){
            $msg = app::get('b2c')->_('手机号码和固定电话至少填写一个');
			return false;
        }

        if($arr_post['consignee_id'] && !app::get('b2c')->model('member_consignee')->getList('consignee_id',array('consignee_id'=>$arr_post['consignee_id'],'member_id'=>$member_id))){
            $msg = app::get('b2c')->_('更新的收货信息不存在');
			return false;
        }
        return  true;
    }

	public function get_receive_consignee(&$controller, $consignee_id='', $member_id=0, $tpl='site/common/rec_consignee.html'){

		if ($consignee_id){
			$arr_cons_id = explode(':', $consignee_id);
			$cons_id = $arr_cons_id[0];
			$last_cons_id = $arr_cons_id[1];

			if ($cons_id != '-1'){
				$member_consignee = &$controller->app->model('member_consignee');
				$arr_member_cons = $member_consignee->dump(array('consignee_id'=>$consignee_id ? $consignee_id : $last_cons_id,'member_id'=>$member_id), '*');
				if ($cons_id == '0' && $last_cons_id != '0'){
					unset($arr_member_cons['consignee_id']);
				}

			}
		}

		$is_rec_cons_edit = 'true';
		$obj_recsave_checkbox = kernel::service('b2c.checkout_recsave_checkbox');
		$controller->pagedata['is_rec_cons_edit'] = $is_rec_cons_edit;
		$controller->pagedata['cons'] = $arr_member_cons;
		$controller->pagedata['consignee']['member_id'] = $member_id;

		$controller->pagedata['is_recsave_display'] = $is_recsave_display;
		$phones=app::get('b2c')->getConf('setting-phone.phone');
		$controller->pagedata['phones'] = $phones;
		$str_html = $controller->fetch($tpl);
		$obj_ajax_view_help = kernel::single('b2c_view_ajax');
		return $obj_ajax_view_help->get_html($str_html, 'b2c_member_consignee','get_receive_consignee');
	}

}

